package com.example.cpms.controller;

import com.example.cpms.entity.Device;
import com.example.cpms.entity.Plant;
import com.example.cpms.service.DeviceService;
import com.example.cpms.service.PlantService;
import com.example.cpms.utils.PageBean;
import com.example.cpms.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 乔
 */
@CrossOrigin
@Slf4j
@RestController
public class PlantController {
    @Autowired
    private PlantService plantService;
    @GetMapping("/plant")  //改用PageHelper插件
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10")Integer pageSize,//前端传入要查询的页数以及每页显示多少条数据
                       String name){ //模糊查询额外传入的值，可选
        log.info("设备分页查询：{}，{},{}",page,pageSize,name); //输出日志信息
        PageBean pageBean = plantService.page(page,pageSize,name); //把前端传入的信息传到service层
        return Result.success(pageBean);//返回数据
    }
    @DeleteMapping("/plant/{id}")
    public Result delete(@PathVariable int id){
        log.info("根据id删除植物：{}",id);
        plantService.delete(id);
        return Result.success();
    }

    @PostMapping("/plant")
    public Result insert(@RequestBody Plant plant){
        log.info("新增植物：{}",plant);//打印日志信息
        plantService.add(plant);
        return Result.success();
    }
    @PutMapping("/plant")
    public Result updata(@RequestBody Plant plant){
        log.info("编辑植物：{}",plant);
        plantService.update(plant);
        return Result.success();
    }
}
